#!/bin/bash

# path
if [ $(scutil --get LocalHostName) = "marcowdeiMac" ];then
    OpS_PATH='/Volumes/T4/OpS'
    MN_IDA_DEST="/Volumes/Other/Study/Develop/Debug/DebugTest/MarginNote3/3.7.11"
elif [ $(scutil --get LocalHostName) = "MBP" ];then 
    OpS_PATH="${HOME}/OpS"
    MN_IDA_DEST="${HOME}/Desktop/DebugTest/MN3/3.7.11"
else
    echo "⚠️  ⚠️  ⚠️  主机名错误！！！ ⚠️  ⚠️  ⚠️"
    exit 68 # EX_NOHOST
fi


RepositoryPaths=(
    "${OpS_PATH}/Debug/DebugStudyDemo"
    "${OpS_PATH}/Debug/TinyToolsSwift"
    "${OpS_PATH}/My/logseq"
    "${OpS_PATH}/My/Note"
    "${OpS_PATH}/My/NoteProject/PDF/APDF"
    )
IDA_APP="/Applications/IDA Pro 7.0/ida64.app"
MN_IDA="MarginNote 3.i64"
MN_IDA_ZIP="${MN_IDA}.zip"
MN_IDA_SOUR=${HOME}/Library/Mobile\ Documents/com\~apple\~CloudDocs


END="============================="

# git push
for path in "${RepositoryPaths[@]}"; do

    pushd "${path}"

    git push

    # 有未提交的文件就生成补丁
    status=`git status -s`
    if [ -n "${status}" ]; then
        name=${path##*/}
        git add .
        annotation="test"
        git commit -m "${annotation}" 
        git format-patch -1
        mv "0001-${annotation}.patch" "${MN_IDA_SOUR}/${name}.patch" 
        git reset --soft HEAD^
        git stash -u
    fi 

    popd
    echo "${path} push OK!"
    
done
echo ${END}


# close ida
osascript -e 'quit app "ida64"'

# zip 
pushd "${MN_IDA_DEST}"
zip "./${MN_IDA_ZIP}" "./${MN_IDA}"
popd

# copy ida
mv "${MN_IDA_DEST}/${MN_IDA_ZIP}" "${MN_IDA_SOUR}"
echo "MN_IDA moved!"
echo ${END}